package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.MallShopListResponse
import com.gexiaobao.pigeon.app.model.bean.RaceVoucherResponse
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

/**
 * @Author      : hxw
 * @Date        : 2024/3/21 8:48
 * @Describe    :
 */
class ProofViewModel : BaseViewModel() {

    private var pageNo: Int = 1
    private val pageSize: Int = 20

    /** 获取比赛凭证 */
    val raceVoucherResult: MutableLiveData<ListDataUiState<RaceVoucherResponse.ListBean>> = MutableLiveData()
    fun getRaceVoucher(isRefresh: Boolean, raceName: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getRaceVoucher(pageNo, pageSize, raceName) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list,
            )
            raceVoucherResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errCode = it.code,
                errMessage = it.msg,
                listData = arrayListOf<RaceVoucherResponse.ListBean>()
            )
            raceVoucherResult.value = listDataUiState
            RxToast.showToast(it.msg)
        })
    }

}